package com.appspot.gaeforum309.db;

import java.util.List;

import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.appspot.gaeforum309.core.ForumAPI;
import com.appspot.gaeforum309.pathing.Action;
import com.appspot.gaeforum309.pathing.PathRoot;
import com.appspot.gaeforum309.pathing.PathableClass;
import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class DBCategory implements IPathable {
	public final static String tagName = "Category";
	
	public static final PathableClass pathable = new PathableClass(tagName, PathRoot.tagName){

		@Override
		public IPathable getObject(String value) {
			return DBCategory.getCategory(value);
		}

		@Override
		public List<? extends IPathable> allObjects() {
			return DBCategory.getAllCategories();
		}
		
	};
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	@Persistent
	private String title;
	
	@Persistent
	private String description;

	public DBCategory(String titre, String description) {
		super();
		this.title = titre;
		this.description = description;
	}

	public String getTitre() {
		return title;
	}

	public void setTitre(String titre) {
		this.title = titre;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Key getKey() {
		return key;
	}
	
	@Override
	public String getKeyStr()
	{
		return KeyFactory.keyToString(getKey());
	}
	
	@Override
	public String getParentKeyStr() {
		return PathRoot.tagKey;
	}
	
	public static DBCategory createCategory(String title, String description)
	{
		DBCategory dbCateg = new DBCategory(title, description);
		PMF.pm().makePersistent(dbCateg);
		return dbCateg;
	}
	
	public static List<DBCategory> getAllCategories()
	{
		Query q = PMF.pm().newQuery(DBCategory.class);
		//q.setFilter("title == argTitle");
		//q.declareParameters("String argTitle");
		@SuppressWarnings("unchecked") // Return type ensure by newQuery parameters
		List<DBCategory> res = (List<DBCategory>) q.execute();
		
		return res;
	}
	
	public static DBCategory getCategory(String keystr)
	{
		Key key = KeyFactory.stringToKey(keystr);
		if(key == null)
			return null;
		
		DBCategory res = (DBCategory)PMF.pm().getObjectById(DBCategory.class, key);
		return res;
	}

	@Override
	public String label() {
		return getTitre();
	}

	@Override
	public PathableClass getPathable() {
		return pathable;
	}

	@Override
	public List<? extends IPathable> getChilds(int fromNumber, int toNumber) {
		return getChilds().subList(fromNumber, toNumber);
	}

	@Override
	public List<? extends IPathable> getChilds() {
		return DBTopic.getAllTopicsOnCategory(this);
	}

	@Override
	public boolean authorizedAction(Action.ActionType actionType) {
		return false;
	}

	@Override
	public boolean applyAction(Action action) {
		// TODO
		return false;
	}
}
